//
//  GPVoiceMessageCell.m
//  Keping
//
//  Created by 23 on 2017/12/22.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPVoiceMessageCell.h"

@interface GPVoiceMessageCell()

/**icon*/
@property(nonatomic,weak) UIImageView *voiceIconView;

/**timeLabel*/
@property(nonatomic,weak) UILabel *voiceTimeLabel;

/**是否未读表示*/
@property(nonatomic,weak) UIView *isUnreadIcon;

@end

@implementation GPVoiceMessageCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIImageView *voiceIconView = [[UIImageView alloc] init];
        [self.contentBackground addSubview:voiceIconView];
        self.voiceIconView = voiceIconView;
        
        UILabel *voiceTimeLabel = [[UILabel alloc] init];
        voiceTimeLabel.text = @"10\"";
        voiceTimeLabel.font = [UIFont systemFontOfSize:12.0];
        voiceTimeLabel.textColor = [UIColor darkGrayColor];
        [self.contentBackground addSubview:voiceTimeLabel];
        self.voiceTimeLabel = voiceTimeLabel;
        
        UIView *isUnreadIcon = [[UIView alloc] init];
        isUnreadIcon.backgroundColor = [UIColor redColor];
        isUnreadIcon.layer.cornerRadius = 2.f;
        isUnreadIcon.layer.masksToBounds = YES;
        [self.contentBackground addSubview:isUnreadIcon];
        self.isUnreadIcon = isUnreadIcon;
        
        [self.contentBackground addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapContent)]];
        
    }
    return self;
}


- (void)setMessage:(GPMessageItem *)message
{
    //设置父类的值和约束
    [super setMessage:message];
    
    self.contentBackground.backgroundColor = [UIColor whiteColor];
    
    [self.contentBackground mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
    }];
    
    
    self.voiceTimeLabel.text = [NSString stringWithFormat:@"%d\"", (int)message.mediaDuration];
    
    if (message.isSender) {
        
        self.voiceIconView.image = [UIImage imageNamed:@"ic_right_voice_play-4"];
        self.voiceTimeLabel.textAlignment = NSTextAlignmentRight;
        self.isUnreadIcon.hidden = YES;
        
        [self.voiceIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.contentBackground).offset(15);
            make.right.mas_equalTo(self.contentBackground).offset(-10);
            make.bottom.mas_equalTo(self.contentBackground).offset(-15);
            make.width.height.mas_equalTo(10);
            
        }];
        
        [self.voiceTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.voiceIconView);
            make.right.mas_equalTo(self.voiceIconView.mas_left).offset(-10);
        }];
        
        [self.isUnreadIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.voiceIconView);
            make.left.mas_equalTo(self.contentBackground).offset(2);
            make.width.height.mas_equalTo(4);
        }];
        
        
    
    }else{
        
        self.voiceIconView.image = [UIImage imageNamed:@"ic_left_voice_play-4"];
        self.voiceTimeLabel.textAlignment = NSTextAlignmentLeft;
        self.isUnreadIcon.hidden = message.isMediaPlayed;
        
        [self.voiceIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.contentBackground).offset(15);
            make.left.mas_equalTo(self.contentBackground).offset(10);
            make.bottom.mas_equalTo(self.contentBackground).offset(-15);
            make.width.height.mas_equalTo(10);
            
        }];
        
        [self.voiceTimeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.voiceIconView);
            make.left.mas_equalTo(self.voiceIconView.mas_right).offset(10);
        }];
        
        [self.isUnreadIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.voiceIconView);
            make.right.mas_equalTo(self.contentBackground).offset(-2);
            make.width.height.mas_equalTo(4);
        }];
    }
    
    
    //更新
    [self layoutIfNeeded];
    
    //计算cell的高度
    CGFloat cellHeight = CGRectGetMaxY(self.contentBackground.frame) + 10;
    message.cellHeight = cellHeight;
}

- (void)tapContent
{
    self.message.isMediaPlayed = YES;
    [self.message.message.ext setValue:@(YES) forKey:@"isPlayed"];
    
    if ([self.delegate respondsToSelector: @selector(messageCell:didContentClicked:)]) {
        [self.delegate messageCell:self didContentClicked:self.message];
    }
}

@end
